package com.myspace.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSRequest.MSRequestCallback;
import com.myspace.util.MSDrawableManager;

public class MSStatusMoodListActivity extends MSBaseActivity {

	private ListView mListView;
	private StatusMoodListAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setProgressBarIndeterminateVisibility(false);
		
		super.onCreate(savedInstanceState);
		super.setPageName(R.string.PAGE_TITLE_STATUS_MOOD);
		super.setMainView(R.layout.statusmood_list);
		
		Spinner spinner = (Spinner) findViewById(R.id.selectorSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.statusMoodSelectors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new SelectorSpinnerListener());
        
        mListView = (ListView) findViewById(R.id.statusMoodList);
		mAdapter = new StatusMoodListAdapter(this);
		mListView.setAdapter(mAdapter);
	}
	
	private class SelectorSpinnerListener implements OnItemSelectedListener {    
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			getStatusMood(pos);
		}    

		public void onNothingSelected(AdapterView<?> parent) {      
		}
	}
	
	private void getStatusMood(int pos) {
		switch(pos) {
			case 0: 
				MSSDK.getStatusMood(new GetStatusMood_MSRequestCallback());
				break;
			case 1: 
				Map<String, String> queryParams = new HashMap<String, String>();
				queryParams.put("filterby", "@topfriends");
				MSSDK.getStatusMoodWithParameters(queryParams, new GetStatusMood_MSRequestCallback());
				break;
			case 2:
				MSSDK.getStatusMoodForFriend("6221", null, new GetStatusMood_MSRequestCallback());
				break;
			case 3:
				MSSDK.getStatusMoodHistoryForFriend("6221", null, new GetStatusMood_MSRequestCallback());
		}
		
		setProgressBarIndeterminateVisibility(true);	
	}

	private class GetStatusMood_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			setProgressBarIndeterminateVisibility(false);
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			setProgressBarIndeterminateVisibility(false);
			
			Map<?,?> data = (Map<?,?>)result;
			result  = data.get("data");
			
			if(result instanceof List<?>) {
				List<StatusMoodItem> items = new ArrayList<StatusMoodItem>();
				List<?> statusList = (List<?>)result;			
				if(statusList != null && statusList.size() > 0) {
					for (int i=0; i < statusList.size(); i++) {
						Map<?,?> statusObject = (Map<?,?>) statusList.get(i);
						StatusMoodItem item = buildStatusMoodItem(statusObject);
						items.add(item);
					}
					updateView(items);
				}			
			}
		}
	}
	
	private StatusMoodItem buildStatusMoodItem(Map<?,?> statusObject) {
		String status = (String)statusObject.get("status");
		String moodName = (String)statusObject.get("moodName");
		String moodImageUrl = (String)statusObject.get("moodPictureUrl");
		String userName = (String)statusObject.get("userName");
		String userProfileURL = (String)statusObject.get("userProfileURL");
		String userThumbnailImageURL = (String)statusObject.get("userThumbnailImageURL");
		
		StatusMoodItem item = 
			new StatusMoodItem(
					status, 
					moodName, 
					moodImageUrl, 
					userName, 
					userProfileURL,
					userThumbnailImageURL
					);
		
		return item;
	}
	
	private void updateView(List<StatusMoodItem> items) {
		mAdapter.clear();
		for (int i=0; i < items.size(); i++) {
			mAdapter.add(items.get(i));
		}
		mAdapter.notifyDataSetChanged();
	}

	private class StatusMoodListAdapter extends ArrayAdapter<StatusMoodItem> {
		private LayoutInflater mInflater;

		public StatusMoodListAdapter(Context context) {
			super(context, 0);
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = mInflater.inflate(R.layout.statusmood_list_item, null);
			
			StatusMoodItem item = this.getItem(position);
			
			TextView v;
			ImageView iv;
			
			if(item.getUserThumbnailImageURL() != null) {
				iv = (ImageView) view.findViewById(R.id.authorImage);
				MSDrawableManager.getInstance().fetchDrawableOnThread(item.getUserThumbnailImageURL(), iv);
			}
				
			v = (TextView) view.findViewById(R.id.statusText);
			String html = String.format("<a href=\"%s\">%s</a> %s", item.getUserProfileURL(), item.getUserName(), item.getStatus());
			v.setText(Html.fromHtml(html), TextView.BufferType.SPANNABLE);
			v.setMovementMethod(LinkMovementMethod.getInstance());
			
			v = (TextView) view.findViewById(R.id.moodText);
			v.setText(item.getMoodName());
			
			if(item.getMoodImageUrl() != null) {
				iv = (ImageView) view.findViewById(R.id.moodImage);
				MSDrawableManager.getInstance().fetchDrawableOnThread(item.getMoodImageUrl(), iv);
			}
				
			return view;
		}
	}

	class StatusMoodItem {
		private String mStatus;
		private String mMoodName;  
		private String mMoodImageUrl;
		private String mUserName;
		private String mUserProfileURL;
		private String mUserThumbnailImageURL;
				
		public StatusMoodItem(
				String status, 
				String moodName, 
				String moodImageUrl, 
				String userName,
				String userProfileURL,
				String userThumbnailImageURL) {
			mStatus = status;
			mMoodName = moodName;
			mMoodImageUrl = moodImageUrl;
			mUserName = userName;
			mUserProfileURL = userProfileURL;
			mUserThumbnailImageURL = userThumbnailImageURL;
		}

		public String getStatus() {
			return mStatus;
		}

		public String getMoodName() {
			return mMoodName;
		}
		
		public String getMoodImageUrl() {
			return mMoodImageUrl;
		}
		
		public String getUserName() {
			return mUserName;
		}
		
		public String getUserProfileURL() {
			return mUserProfileURL;
		}
		
		public String getUserThumbnailImageURL() {
			return mUserThumbnailImageURL;
		}
	}
}
